<script setup>
import GoodsItem from "../components/GoodsItem.vue";
import EvaluationItem from "../components/EvaluationItem.vue";
import { ref, computed } from "vue";
import { useUser } from "@/store/useUser.js";
import { getGoodsByStatus } from "@/api/user";
import { getCommentList } from "@/api/user";
import {educationConfig} from "@/utils/appData"

const userStore = useUser();
const userInfo = computed(() => userStore.getUserInfo);

const commentType = ref(null);
const orderBy = ref(0);
const pageSize = ref(5);
const pageNum = ref(1);

const totalCount = ref(0);
const goodCount = ref(0);
const neutralCount = ref(0);
const negativeCount = ref(0);

// 关注人数
const focusCount = computed(() => {
  if (userInfo.value &&userInfo.value.buyerInfo && userInfo.value.buyerInfo.concernCount) {
    return userInfo.value.buyerInfo.concernCount;
  }
  return 0;
});

const commentList = ref([]);
const getCommentTypeList = (type) => {
  commentType.value = type;
  pageNum.value = 1;
  getComments();
};

const activeSort = (type) => {
  orderBy.value = type;
  pageNum.value = 1;
  getComments();
};

const currentChange = (page) => {
  pageNum.value = page;
  getComments();
};

// 获取评价列表
const loading = ref(false);
const getComments = async () => {
  try {
    loading.value = true;
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      type: commentType.value,
      orderBy: orderBy.value,
      // 注意: saleUserId 需要从用户状态或其他地方获取
      saleUserId: userInfo.value.userId,
    };
    if (!params.type) delete params.type;
    const res = await getCommentList(params);
    if (res.code === 200) {
      commentList.value = res.data.commentList;
      if (!totalCount.value) totalCount.value = Number(res.data.totalCount);
      if (!goodCount.value) goodCount.value = Number(res.data.goodCount);
      neutralCount.value = Number(res.data.neutral_count) || 0;
      negativeCount.value = Number(res.data.negative_count) || 0;
    } else {
      // 处理错误
      console.error(res.msg);
    }
  } catch (error) {
    console.error("获取评价列表失败", error);
  } finally {
    loading.value = false;
  }
};
getComments();

// 获取商品列表
const goodsList = ref([]);
const goodsLoading = ref(false);
const total = ref(0);
async function fetchGoodsList() {
  goodsLoading.value = true;
  try {
    const response = await getGoodsByStatus({
      queryStatus: 1,
      pageNum: 1,
      pageSize: 100,
    });
    goodsList.value = response.data.records;
    total.value = response.data.total;
  } catch (error) {
    console.error("获取商品列表失败:", error);
  } finally {
    goodsLoading.value = false;
  }
}
fetchGoodsList();

const educationsData = computed(()=>{
  if(userInfo.value.salerInfo && userInfo.value.salerInfo.educations){
    const educations = userInfo.value.salerInfo.educations.sort((a,b)=> b.graduateTime - a.graduateTime)
    return educations[0]
  }
  return {}
})
</script>

<template>
  <div class="user">
    <div class="user-info">
      <img
        v-if="userInfo && userInfo.avatar"
        class="head"
        :src="userInfo.avatar"
        alt=""
      />
      <img v-else class="head" src="@/assets/logo-small.png" alt="" />
      <div class="info">
        <p class="name">
          <span class="name-txt">{{
            userInfo.nickName || userInfo.phonenumber
          }}</span>
          <span class="focus">{{ focusCount }}人关注</span>
        </p>
        <p class="career">
          <span class="school">{{educationsData.schoolName}}·{{ educationConfig.find(item=> item.value === educationsData.type)['label'] }}</span>
          <span class="btn" v-for="item of userInfo.salerInfo.skill">{{ item.skillName }}</span>
        </p>
      </div>
    </div>
    <div class="user-data" v-if="userInfo && userInfo.salerInfo">
      <div class="item">
        <p>信用等级</p>
        <p>{{ userInfo.salerInfo.creditLevel }}</p>
        <p>分值 {{ userInfo.salerInfo.creditScore }}</p>
      </div>
      <span class="line"></span>
      <div class="item">
        <p>总销量</p>
        <p>{{ userInfo.salerInfo.orderCount }}</p>
        <p>准时交付率 {{ userInfo.salerInfo?.deliveryRate || 0}}%</p>
      </div>
      <span class="line"></span>
      <div class="item">
        <p>好评率</p>
        <p>{{ userInfo.salerInfo?.commentRate }}</p>
        <p>好评数 {{ userInfo.salerInfo.goodsCommentCount || 0 }}</p>
      </div>
    </div>
  </div>
  <h3 class="title-h">商品 ({{ total || 0 }})</h3>
  <div class="goods-list" v-loading="goodsLoading">
    <GoodsItem v-for="item in goodsList" :key="item.id" :goodsItem="item" />
  </div>
  <div class="evaluate-list">
    <div class="evaluate-page">
      <div class="header">
        <p class="title">获得评价（{{ totalCount || 0 }}）</p>
        <div class="sort-box">
          <p class="filter">
            <span
              :class="{ active: !commentType }"
              @click="getCommentTypeList(0)"
              >全部({{ totalCount || 0 }})</span
            >
            <span
              :class="{ active: commentType === 1 }"
              @click="getCommentTypeList(1)"
              >好评({{ goodCount || 0 }})</span
            >
            <span
              :class="{ active: commentType === 2 }"
              @click="getCommentTypeList(2)"
              >中评({{ neutralCount || 0 }})</span
            >
            <span
              :class="{ active: commentType === 3 }"
              @click="getCommentTypeList(3)"
              >差评({{ negativeCount || 0 }})</span
            >
          </p>
          <div class="sort">
            <span :class="{ active: orderBy === 0 }" @click="activeSort(0)"
              >默认排序</span
            >
            <span :class="{ active: orderBy === 1 }" @click="activeSort(1)"
              >最新评价</span
            >
          </div>
        </div>
      </div>
      <div v-loading="loading">
        <EvaluationItem
          v-for="item in commentList"
          :key="item.id"
          :itemData="item"
        />
      </div>
      <div class="paging-box">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pageSize"
          :total="totalCount"
          :current-page="pageNum"
          @current-change="currentChange"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.user {
  display: flex;
  align-items: center;
  background: #ffffff;
  border-radius: 16px 16px 16px 16px;
  border: 1px solid rgba(25, 30, 40, 0.08);
  padding: 30px 20px;
}

.user-info {
  flex: 1;
  display: flex;
  align-items: center;

  .head {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    margin-right: 12px;
  }

  .info {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 55px;

    .name-txt {
      color: #191e28;
      font-size: 20px;
      font-weight: bold;
    }

    .focus {
      color: rgba(25, 30, 40, 0.48);
      margin-left: 12px;
    }

    .school {
      color: #191e28;
      margin-right: 8px;
    }

    .btn {
      padding: 2px 8px;
      color: rgba(25, 30, 40, 0.48);
      border: 1px solid rgba(25, 30, 40, 0.08);
      font-size: 12px;
      border-radius: 4px 4px 4px 4px;
      margin-right: 4px;
    }
  }
}

.user-data {
  display: flex;
  align-items: center;

  .line {
    width: 1px;
    height: 40px;
    background: rgba(25, 30, 40, 0.08);
    margin: 0 40px;
  }

  .item {
    p:nth-child(1) {
      font-size: 12px;
      color: rgba(25, 30, 40, 0.48);
    }

    p:nth-child(2) {
      font-size: 18px;
      color: #191e28;
      font-weight: 600;
      margin: 3px 0;
    }

    p:nth-child(3) {
      font-size: 12px;
      color: rgba(25, 30, 40, 0.48);
    }
  }
}

.title-h {
  margin: 32px 0 16px;
}
.goods-list {
  display: grid;
  grid-template-columns: repeat(
    auto-fill,
    minmax(260px, 1fr)
  ); /* 根据需要调整列宽 */
  gap: 10px; /* 设置网格项之间的间隙 */
}
.evaluate-list {
  flex: 1;
  background: #ffffff;
  border-radius: 16px 16px 16px 16px;
  border: 1px solid rgba(25, 30, 40, 0.08);
  margin-top: 16px;
  overflow-y: auto;
  padding: 40px;
  box-sizing: border-box;
  .evaluate-page {
    max-width: 1200px;
  }
  .header {
    .title {
      color: #191e28;
      font-size: 20px;
      font-weight: bold;
      margin-bottom: 20px;
    }
    .sort-box {
      display: flex;
      align-items: center;
      span {
        line-height: 12px;
        cursor: pointer;
        &:nth-child(1) {
          padding-right: 12px;
          margin-right: 12px;
          border-right: 1px solid rgba(25, 30, 40, 0.08);
        }
      }
      .sort {
        color: #191e28;
      }
      .active {
        color: #1dbf73;
        font-weight: bold;
      }
    }
    .filter {
      flex: 1;
      margin-bottom: 20px;
      span {
        padding: 4px 8px;
        color: rgba(25, 30, 40, 0.6);
        background: rgba(25, 30, 40, 0.02);
        border-radius: 6px 6px 6px 6px;
        margin-right: 12px;
        &.active {
          color: #1dbf73;
          background: rgba(29, 191, 115, 0.08);
        }
      }
    }
  }
}
.paging-box {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 20px;
}
</style>